home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pavt150.zip / CAPI.ZIP / JAMSCAN.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  9KB  |  259 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Joaquim Homrighausen.
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamscan.c (JAMmb)
  11. **
  12. **  Scan message headers and call user comparison routine
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    JoHo
  18. **  Initial coding. No decryption or unescaping supported yet.
  19. */
  20. #define JAMCAPI 1
  21.  
  22. #include "jammb.h"
  23.  
  24. /*
  25. **  Scan message headers starting at the specified number (WhatMsg). For
  26. **  each header found, the function calls USERCOMPARE. The ScanFwd parameter
  27. **  determines the direction of the scan. The Hdr record will contain the
  28. **  newly read message header and if the header had any subfields, WorkBuf
  29. **  will contain as much subfield data as will fit. If all of the subfield
  30. **  data didn't fit and the USERCOMPARE function returns ScanMsgHdrDiscard,
  31. **  the function will read the remaining subfield data and call the user
  32. **  function again. Returns 1 if user function returned ScanMsgHdrStop and
  33. **  FALSE otherwise.
  34. */
  35. int _JAMPROC JAMmbScanForMsgHdr(JAMAPIRECptr apirec, UINT32 WhatMsg,
  36.                         int ScanFwd, ScanMsgHdrComp UserCompare)
  37. {
  38.     int     UserResult;
  39.     INT32   JunkL, MaxBlockToRead;
  40.     UINT32  ReadBytes;
  41.  
  42.     /* Make sure the message number is valid */
  43.     if (WhatMsg<apirec->HdrInfo.BaseMsgNum)
  44.         {
  45.         apirec->APImsg=JAMAPIMSG_INVMSGNUM;
  46.         return (0);
  47.         }
  48.  
  49.     while (1)
  50.         {
  51.         /* Seek to position for current message */
  52.         if (!JAMmbFetchMsgIdx(apirec, WhatMsg))
  53.             return (0);
  54.         if (apirec->SeekFunc(apirec, apirec->HdrHandle, JAMSEEK_SET, apirec->Idx.HdrOffset)!=(INT32)apirec->Idx.HdrOffset)
  55.             {
  56.             apirec->APImsg=JAMAPIMSG_SEEKERROR;
  57.             return (0);
  58.             }
  59.  
  60.         JunkL=apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->Hdr, (INT32)sizeof(JAMHDR));
  61.         if (JunkL==(INT32)sizeof(JAMHDR))
  62.             {
  63.             apirec->LastMsgNum=WhatMsg;
  64.  
  65.             /* If all subfield data will fit, read it all and call user function */
  66.             if (apirec->Hdr.SubfieldLen<=apirec->WorkLen)
  67.                 {
  68.                 if (apirec->Hdr.SubfieldLen>0)
  69.                     {
  70.                     if (apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->WorkBuf, (INT32)apirec->Hdr.SubfieldLen)!=(INT32)apirec->Hdr.SubfieldLen)
  71.                         {
  72.                         apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  73.                         return (0);
  74.                         }
  75.                     }
  76.                 /* Call user function and process result */
  77.                 UserResult=UserCompare(apirec);
  78.                 if (UserResult==ScanMsgHdrStop)
  79.                     {
  80.                     return (1);
  81.                     }
  82.                 else
  83.                     {
  84.                     if (ScanFwd)
  85.                         WhatMsg++;
  86.                     else
  87.                         {
  88.                         if (WhatMsg==apirec->HdrInfo.BaseMsgNum)
  89.                             {
  90.                             apirec->APImsg=JAMAPIMSG_NOMOREMSGS;
  91.                             return (0);
  92.                             }
  93.                         else
  94.                             WhatMsg--;
  95.                         }
  96.                     }
  97.                 }
  98.             else
  99.                 /* All subfield data won't fit, do segmented read/calls */
  100.                 {
  101.                 ReadBytes=0L;
  102.                 MaxBlockToRead=(INT32)apirec->WorkLen;
  103.  
  104.                 do
  105.                     {
  106.                     if (apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->WorkBuf, MaxBlockToRead)!=MaxBlockToRead)
  107.                         {
  108.                         apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  109.                         return (0);
  110.                         }
  111.  
  112.                     if (MaxBlockToRead>0L)
  113.                         UserResult=UserCompare(apirec);
  114.  
  115.                     ReadBytes+=MaxBlockToRead;
  116.                     if ((INT32)(apirec->Hdr.SubfieldLen-ReadBytes)<MaxBlockToRead)
  117.                         MaxBlockToRead=(INT32)(apirec->Hdr.SubfieldLen-ReadBytes);
  118.                     }
  119.                 while (ReadBytes<apirec->Hdr.SubfieldLen &&
  120.                             MaxBlockToRead>0L &&
  121.                                 UserResult==ScanMsgHdrDiscard);
  122.  
  123.                 /* We've read all subfield data or got told to proceed */
  124.  
  125.                 if (UserResult==ScanMsgHdrStop)
  126.                     {
  127.                     return (1);
  128.                     }
  129.                 else
  130.                     {
  131.                     if (ScanFwd)
  132.                         WhatMsg++;
  133.                     else
  134.                         {
  135.                         if (WhatMsg==apirec->HdrInfo.BaseMsgNum)
  136.                             {
  137.                             apirec->APImsg=JAMAPIMSG_NOMOREMSGS;
  138.                             return (0);
  139.                             }
  140.                         else
  141.                             WhatMsg--;
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         else
  147.             {
  148.             /* Check for end of file, otherwise error */
  149.             if (JunkL==0L && ScanFwd)
  150.                 apirec->APImsg=JAMAPIMSG_NOMOREMSGS;
  151.             else 
  152.                 apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  153.             return (0);
  154.             }
  155.         }/*while*/
  156.  
  157.     /* Dummy return to avoid warnings from some compilers */
  158.     return (0);
  159. }
  160.  
  161. /*
  162. **  Scan message index records starting at the specified number (STARTNUM).
  163. **  For each record found, the function calls USERCOMPARE. The ScanFwd
  164. **  parameter determines the direction of the scan. The Idx record will
  165. **  contain the newly read index record. Returns 1 if user function returned
  166. **  ScanMsgHdrStop and FALSE otherwise.
  167. */
  168. int _JAMPROC JAMmbScanForMsgIdx(JAMAPIRECptr apirec, UINT32 WhatMsg,
  169.                         int ScanFwd, ScanMsgIdxComp UserCompare)
  170. {
  171.     int   UserResult;
  172.     INT32 WhatOffset, ReadCount;
  173.  
  174.     /* Make sure it's open */
  175.     if (!apirec->isOpen)
  176.         {
  177.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  178.         return (0);
  179.         }
  180.  
  181.     /* Make sure the message number is valid */
  182.     if (WhatMsg<apirec->HdrInfo.BaseMsgNum)
  183.         {
  184.         apirec->APImsg=JAMAPIMSG_INVMSGNUM;
  185.         return (0);
  186.         }
  187.  
  188.     WhatOffset=(INT32)((WhatMsg-apirec->HdrInfo.BaseMsgNum) * (INT32)sizeof(JAMIDXREC));
  189.  
  190.     /* Seek to position if we're going forward */
  191.     if (ScanFwd)
  192.         {
  193.         if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, WhatOffset)!=WhatOffset)
  194.             {
  195.             apirec->APImsg=JAMAPIMSG_SEEKERROR;
  196.             return (0);
  197.             }
  198.         }
  199.  
  200.     /* Fetch index records */
  201.     while (1)
  202.         {
  203.         /* Seek to position if we're going backward */
  204.         if (!ScanFwd)
  205.             {
  206.             if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, WhatOffset)!=WhatOffset)
  207.                 {
  208.                 apirec->APImsg=JAMAPIMSG_SEEKERROR;
  209.                 return (0);
  210.                 }
  211.             }
  212.  
  213.         ReadCount=apirec->ReadFunc(apirec, apirec->IdxHandle, &apirec->Idx, (INT32)sizeof(JAMIDXREC));
  214.         if (ReadCount==(INT32)sizeof(JAMIDXREC))
  215.             {
  216.             apirec->LastMsgNum=WhatMsg;
  217.  
  218.             /* Call user function and process result */
  219.             UserResult=UserCompare(apirec);
  220.             if (UserResult==ScanMsgIdxStop)
  221.                 {
  222.                 return (1);
  223.                 }
  224.             else
  225.                 {
  226.                 if (ScanFwd)
  227.                     WhatMsg++;
  228.                 else
  229.                     {
  230.                     if (WhatMsg==apirec->HdrInfo.BaseMsgNum)
  231.                         {
  232.                         apirec->APImsg=JAMAPIMSG_NOMOREMSGS;
  233.                         return (0);
  234.                         }
  235.                     else
  236.                         {
  237.                         WhatMsg--;
  238.                         WhatOffset-=(INT32)sizeof(JAMIDXREC);
  239.                         }
  240.                     }
  241.                 }
  242.             }
  243.         else
  244.             {
  245.             /* Check for end of file, otherwise error */
  246.             if (ReadCount==0L && ScanFwd)
  247.                 apirec->APImsg=JAMAPIMSG_NOMOREMSGS;
  248.             else 
  249.                 apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  250.             return (0);
  251.             }
  252.         }/* while */
  253.  
  254.     /* Dummy return to avoid warnings from some compilers */
  255.     return (0);
  256. }
  257.  
  258. /* end of file "jamscan.c" */
  259.